﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreDemo.Model;
using CoreDome.Services;
using Microsoft.AspNetCore.Mvc;

namespace CoreDome.Controllers
{
    public class MovieController : Controller
    {
        private readonly IMovieService movieService;
        private readonly ICinemaService cinemaService;

        public MovieController(IMovieService movieService,ICinemaService cinemaService)
        {
            this.movieService = movieService;
            this.cinemaService = cinemaService;
        }

        public async Task<IActionResult> Index(int cinemaId=1)
        {
            var cinema = await cinemaService.GetByIdAsync(cinemaId);
            ViewBag.Title = $"{cinema.Name}这个电影院上映的电影有：";
            ViewBag.CinemaId = cinemaId;
            return View(await movieService.GetByCinemaIdAsync(cinemaId));
        }

        public IActionResult Add(int cinemaId)
        {
            ViewBag.Title = "添加电影";
            return View(new Movie() { CinemaId=cinemaId});
        }
        [HttpPost]
        public async Task<IActionResult> Add(Movie movie)
        {
            if (ModelState.IsValid)
            {
                await movieService.AddAsync(movie);
            }
            return RedirectToAction(nameof(Index),new { cinemaId = movie.CinemaId});
        }

    }
}